package com.echo.usedsfu;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Logger;

import javax.jdo.PersistenceManager;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.echo.usedsfu.entity.Information;
import com.echo.usedsfu.persistence.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

public class UpdateAdServlet extends HttpServlet {
	private static final Logger log = Logger.getLogger(UpdateAdServlet.class.getName());
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)throws IOException{
		String info_id = req.getParameter("info_id");
		Key k = KeyFactory.createKey(Information.class.getSimpleName(), Long.parseLong(info_id));
		
		String title = req.getParameter("title");
		String area = req.getParameter("area");
		String contact = req.getParameter("contact");
		String description = req.getParameter("description");
		String phone = req.getParameter("phone");
		String email = req.getParameter("email");
		String temp_time = req.getParameter("expire_time");
		Date pub_time = new Date();
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			DateFormat formatter = new SimpleDateFormat("MM/DD/yyyy");
		    Date expire_time = (Date)formatter.parse(temp_time);
		    Information update_info = pm.getObjectById(Information.class, k);
		    
		    update_info.setTitle(title);
		    update_info.setArea(area);
		    update_info.setContact(contact);
		    update_info.setDescription(description);
		    update_info.setPhone(phone);
		    update_info.setEmail(email);
		    update_info.setPub_time(pub_time);
		    update_info.setExpire_time(expire_time);
		}catch(ParseException e){
			System.err.println(e);
		}finally{
			pm.close();
		}
		resp.sendRedirect("/index.jsp");
	}
}
